package com.example.software.controller;

import com.example.software.entity.WorkerInfo;
import com.example.software.service.WorkerInfoService;
import com.example.software.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/worker")
public class WorkerController {
    
    private static final Logger log = LoggerFactory.getLogger(WorkerController.class);

    @Autowired
    private WorkerInfoService workerInfoService;

    /**
     * 获取零工信息
     * @param request HTTP请求
     * @return 零工信息
     */
    @GetMapping("/info")
    public Result<WorkerInfo> getWorkerInfo(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        String userType = (String) request.getAttribute("userType");
        
        // 验证用户类型
        if (!"worker".equals(userType)) {
            return Result.error("非零工用户无权访问零工信息");
        }
        
        WorkerInfo workerInfo = workerInfoService.getByUserId(userId);
        return Result.success(workerInfo);
    }

    /**
     * 更新零工信息
     * @param workerInfo 零工信息
     * @param request HTTP请求
     * @return 更新结果
     */
    @PutMapping("/info")
    public Result<Boolean> updateWorkerInfo(@RequestBody WorkerInfo workerInfo, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        String userType = (String) request.getAttribute("userType");
        
        // 验证用户类型
        if (!"worker".equals(userType)) {
            return Result.error("非零工用户无权更新零工信息");
        }
        
        // 设置用户ID，确保只能更新自己的信息
        workerInfo.setUserId(userId);
        
        boolean result = workerInfoService.updateWorkerInfo(workerInfo);
        return Result.success(result);
    }
} 